﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class NewsList : System.Web.UI.Page
{
    public string smallClassName, bigClassName, contentHtml, liHtml, newsTitle, addTime, newsCount;
    string bigClassID = string.Empty;
    string smallClassID = string.Empty;
    string page = string.Empty;
    int titleLength = 90;
    string pageView = "newscontent.aspx";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["bigclassid"] != null) { bigClassID = Request.QueryString["bigclassid"].ToString(); }
        if (Request.QueryString["smallclassid"] != null) { smallClassID = Request.QueryString["smallclassid"].ToString(); }
        if (IsNumeric(bigClassID) == false) { bigClassID = "8"; }
        if (IsNumeric(smallClassID) == false) { smallClassID = "36"; }

        Page_newsList.RecordCount = (int)Socut.Data.ExecuteScalar(string.Format(@"
select count(*) 
from web_notice  
where smallclassid = {0}",smallClassID));

        bigClassName = Socut.Data.ExecuteScalar(string.Format(@"
select bigclassname
from web_notice_bigclass  
where id = {0}", bigClassID)).ToString();

        //内容
        contentHtml = ShowData();

        //菜单
        DataSet dslink = Socut.Data.ExecuteDataSet(string.Format(@"
SELECT id,smallclassname 
FROM dbo.web_notice_smallclass 
WHERE bigclassid={0} 
ORDER BY smallclassorder", bigClassID));
        StringBuilder liBuilder = new StringBuilder();
        foreach (DataRow row in dslink.Tables[0].Rows)
        {
            int count = (int)Socut.Data.ExecuteScalar(string.Format(@"
select count(*) 
from web_notice  
where smallclassid = {0}", row["id"].ToString()));
            string goPage = string.Format("NewsList.aspx?bigclassid={0}&smallclassid={1}", bigClassID, row["id"].ToString());
            if (count == 1)
            {
                string id = Socut.Data.ExecuteScalar(string.Format(@"
select id
from web_notice  
where smallclassid = {0}", row["id"].ToString())).ToString();
                goPage = string.Format("NewsContent.aspx?id={0}",id);
            }

            if (row["id"].ToString() == smallClassID)
            {
                smallClassName = row["smallclassname"].ToString();
                liBuilder.Append(string.Format("<li><a href=\"{0}\" class=\"content-left-current\">{1}</a></li>",
                    goPage, row["smallclassname"].ToString()));
            }
            else
            {
                liBuilder.Append(string.Format("<li><a href=\"{0}\">{1}</a></li>",
                    goPage, row["smallclassname"].ToString()));
            }
        }
        liHtml = liBuilder.ToString();
    }

    protected void Page_newsList_PageChanged(object sender, EventArgs e)
    {
        contentHtml = ShowData();
    }

    private string ShowData()
    {
        string sql = string.Format(@"
SELECT id,title,addtime FROM web_notice
where smallclassid={0}
order by addtime desc,id desc",smallClassID);
        DataSet ds = Socut.Data.ExecuteDataSet(sql, 
            Page_newsList.PageSize * (Page_newsList.CurrentPageIndex - 1), 
            Page_newsList.PageSize);
        
        StringBuilder newsBuilder = new StringBuilder();
        foreach(DataRow row in ds.Tables[0].Rows)
        {
            string news = string.Format("<li><span>{0}</span><a href=\"{3}?ID={1}\" target=\"_blank\">{2}</a></li>",
                DateTime.Parse(row["addtime"].ToString()).ToShortDateString(), row["id"].ToString(),
                CutString(row["title"].ToString(), titleLength),pageView);
            newsBuilder.Append(news);
        }

        return newsBuilder.ToString();
    }

    private Hashtable ShowData(string fieldString, string sql)
    {
        Hashtable htResult = new Hashtable();
        Socut.Reader sqlDataReader = new Socut.Reader(sql);
        try
        {
            sqlDataReader.Read();

            string[] fieldList = fieldString.Split(',');
            foreach (string fieldName in fieldList)
            {
                htResult.Add(fieldName, htmlstr(sqlDataReader[fieldName].ToString()));
            }
        }
        catch (Exception ex)
        {
            Response.Write("为保证网站的服务质量，服务器临时维护中...请稍候!" + ex.Message);
            Response.End();
        }

        sqlDataReader.Close();
        return htResult;
    }

    public string htmlstr(string chr)
    {
        if (chr == null)
            return "";
        chr = chr.Replace("\n", "<br>");
        chr = chr.Replace("select", "");
        chr = chr.Replace("delete", "");
        chr = chr.Replace("update", "");
        chr = chr.Replace("drop", "");
        chr = chr.Replace("and", "");
        chr = chr.Replace("or", "");
        return (chr);
    }

    static bool IsNumeric(string str)
    {
        if (str == null || str.Length == 0)
            return false;
        foreach (char c in str)
        {
            if (!Char.IsNumber(c))
            {
                return false;
            }
        }
        return true;
    }

    public string CutString(string str, int length)
    {
        int i = 0, j = 0;
        foreach (char chr in str)
        {
            if ((int)chr > 127)
            {
                i += 2;
            }
            else
            {
                i++;
            }
            if (i > length)
            {
                str = str.Substring(0, j) + "…";
                break;
            }
            j++;
        }
        return str;
    } 
}
